UCV2013H - Slick

题目 UCV2013H - Slick

image-95c9eb32

思路分析

额外维护一个面积 应该可以用map来做

真的是写一题骂一次洛谷 显示个勾八 Unknown error 交题解代码也是这b样

代码实现

#include<bits/stdc++.h>

using namespace std;

#define endl '\n'

typedef pair<int,int> PII;

const int N=255;

int g[N][N];

int n,m;

bool st[N][N];

int cnt;

map<int,int> Hash;

int dx[4]={-1,0,1,0};

int dy[4]={0,1,0,-1};

bool isVaild(int x,int y){

	return x>=0 && x<=n-1 && y>=0 && y<=m-1 && !st[x][y];

}

void bfs(int x,int y){

	queue<PII> q;

	st[x][y]=true;

	q.push({x,y});

	int Size=0;

	while(q.size()){

		auto cur=q.front();q.pop();

		int ux=cur.first,uy=cur.second;

		Size++;

		for(int i=0;i<4;i++){

			int nx=ux+dx[i],ny=uy+dy[i];

			if(isVaild(nx,ny) && g[nx][ny]==1){

				st[nx][ny]=true;

				q.push({nx,ny});

			}

		}

	}

	Hash[Size]++;

}

int main()

{

	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

	while(cin>>n>>m,n,m){

		for(int i=0;i<n;i++){

			for(int j=0;j<m;j++){

				cin>>g[i][j];

			}

		}

		cnt=0;

		Hash.clear();

		memset(st,false,sizeof st);

		for(int i=0;i<n;i++){

			for(int j=0;j<m;j++){

				if(g[i][j]==1 && !st[i][j]){

					bfs(i,j);

					cnt++;

				}

			}

		}

		cout<<cnt<<endl;

		for(auto x:Hash){

			cout<<x.first<<" "<<x.second<<endl;

		}

	}

	return 0;

}

同类题型

视频讲解


⬅️ Flood Fill(BFS版) 🏠 00-刷题理模型 ➡️ 家族